package aidturith.db.configuration;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import aidturith.db.beans.Album;
import aidturith.db.beans.Alias;
import aidturith.db.beans.Artist;
import aidturith.db.beans.Circle;
import aidturith.db.beans.Event;
import aidturith.db.beans.File;
import aidturith.db.beans.Game;
import aidturith.db.beans.Genre;
import aidturith.db.beans.Music;
import aidturith.db.beans.Serie;
import aidturith.db.beans.Subtitle;
import aidturith.db.hibernate.HibernateUtil;

/**
 * Classe de test pour la base de données.
 * @author Guillaume Pocas & Jean-Fraçois Augé
 */
public class FirstConfiguration {

	@SuppressWarnings("unchecked")
	public static void clean() {
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();

		List<Serie> list1 = session.createCriteria(Serie.class).list();
		for(Serie o : list1) {
			System.out.println("delete "+o);
			session.delete(o);
		}

		List<Game> list2 = session.createCriteria(Game.class).list();
		for(Game o : list2) {
			System.out.println("delete "+o);
			session.delete(o);
		}

		List<Subtitle> list3 = session.createCriteria(Subtitle.class).list();
		for(Subtitle o : list3) {
			System.out.println("delete "+o);
			session.delete(o);
		}

		List<Genre> list4 = session.createCriteria(Genre.class).list();
		for(Genre o : list4) {
			System.out.println("delete "+o);
			session.delete(o);
		}

		List<Artist> list6 = session.createCriteria(Artist.class).list();
		for(Artist o : list6) {
			System.out.println("delete "+o);
			session.delete(o);
		}

		List<Circle> list7 = session.createCriteria(Circle.class).list();
		for(Circle o : list7) {
			System.out.println("delete "+o);
			session.delete(o);
		}

		List<Event> list8 = session.createCriteria(Event.class).list();
		for(Event o : list8) {
			System.out.println("delete "+o);
			session.delete(o);
		}

		List<Album> list9 = session.createCriteria(Album.class).list();
		for(Album o : list9) {
			System.out.println("delete "+o);
			session.delete(o);
		}

		List<Music> list10 = session.createCriteria(Music.class).list();
		for(Music o : list10) {
			System.out.println("delete "+o);
			session.delete(o);
		}

		List<Alias> list11 = session.createCriteria(Alias.class).list();
		for(Alias o : list11) {
			System.out.println("delete "+o);
			session.delete(o);
		}

		tx.commit();
		session.close();
	}

	/**
	 * Données de tests pour la base de données.
	 */
	public static void build() {
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();

		Serie s1 = new Serie("Touhou");
		session.save(s1);
		Serie s2 = new Serie("Zelda");
		session.save(s2);
		Serie s3 = new Serie("Mario");
		session.save(s3);
		Serie s4 = new Serie("Pokémon");
		session.save(s4);

		s1.addAlias(new Alias("Touhou Project"));
		s1.addAlias(new Alias("Toho Project"));
		s1.addAlias(new Alias("東方Project"));
		s1.addAlias(new Alias("Eastern Project"));
		s1.addAlias(new Alias("Project Shrine Maiden"));
		session.save(s1);

		ArrayList<Game> game_list = new ArrayList<Game>();
		game_list.add(new Game("Highly Responsive to Prayers", 1996));
		game_list.add(new Game("Phantasmagoria of Dim.Dream", 1997));
		game_list.add(new Game("the Story of Eastern Wonderland", 1997));
		game_list.add(new Game("Mystic Square", 1998));
		game_list.add(new Game("Lotus Land Story", 1998));
		game_list.add(new Game("the Embodiment of Scarlet Devil.", 2002));
		game_list.add(new Game("Perfect Cherry Blossom", 2003));
		game_list.add(new Game("Imperishable Night", 2004));
		game_list.add(new Game("Immaterial and Missing Power", 2004));
		game_list.add(new Game("Shoot the Bullet", 2005));
		game_list.add(new Game("Phantasmagoria of Flower View", 2005));
		game_list.add(new Game("Mountain of Faith", 2007));
		game_list.add(new Game("Subterranean Animism", 2008));
		game_list.add(new Game("Scarlet Weather Rhapsody", 2008));
		game_list.add(new Game("Undefined Fantastic Object", 2009));
		game_list.add(new Game("Double Spoiler", 2010));
		game_list.add(new Game("Great Fairy Wars", 2010));
		game_list.add(new Game("Ten Desires", 2011));
		game_list.add(new Game("Hopeless Masquerade", 2013));
		game_list.add(new Game("Double Dealing Character"));

		game_list.get(17).addAlias(new Alias("Shinreibyou"));
		session.save(game_list.get(17));

		for(Game g : game_list) {
			s1.addGame(g);
			session.save(g);
		}
		session.save(s1);

		ArrayList<Subtitle> subtitle_list = new ArrayList<Subtitle>();
		subtitle_list.add(new Subtitle("Spirit of Avarice"));
		subtitle_list.add(new Subtitle("Night Sakura of Dead Spirits"));
		subtitle_list.add(new Subtitle("Ghost Lead"));
		subtitle_list.add(new Subtitle("Welcome to Youkai Temple"));
		subtitle_list.add(new Subtitle("A Youkai Girl in Front of the Gate"));
		subtitle_list.add(new Subtitle("Let's Live in Nice Cemetery"));
		subtitle_list.add(new Subtitle("Rigid Paradise"));
		subtitle_list.add(new Subtitle("Desire Drive"));
		subtitle_list.add(new Subtitle("Old Distant Hermit"));
		subtitle_list.add(new Subtitle("Dream Palace Great Mausoleum"));
		subtitle_list.add(new Subtitle("The Legend of the Great Gods"));
		subtitle_list.add(new Subtitle("A Starry Sky of Small Desires"));
		subtitle_list.add(new Subtitle("Shoutoku Legend ~ True Administrator"));
		subtitle_list.add(new Subtitle("Youkai Back Shrine Road"));
		subtitle_list.add(new Subtitle("Futatsuiwa from Sado"));
		subtitle_list.add(new Subtitle("A New Wind at the Shrine"));
		subtitle_list.add(new Subtitle("Desire Dream"));

		for(Subtitle st : subtitle_list) {
			game_list.get(17).addSubtitle(st);
			session.save(st);
		}
		session.save(game_list.get(17));

		Genre ge2 = new Genre("Jazz", "Acoustic");
		session.save(ge2);
		Genre ge4 = new Genre("House", "Electronic");
		session.save(ge4);

		ArrayList<Artist> artist_list = new ArrayList<Artist>();
		artist_list.add(new Artist("ZUN", "Male", "Japan"));
		artist_list.add(new Artist("Hiroki Kikuta", "Male", "Japan"));
		artist_list.add(new Artist("Hibiki", "Male", "Australia"));
		artist_list.add(new Artist("Ghost", "Male", "USA"));

		artist_list.get(0).addAlias(new Alias("Jun'ya Ōta"));
		artist_list.get(0).addAlias(new Alias("太田 順也"));
		artist_list.get(1).addAlias(new Alias("菊田 裕樹"));
		artist_list.get(2).addAlias(new Alias("Kevin Penkin"));
		artist_list.get(3).addAlias(new Alias("Alex Vourtsanis"));

		Circle c1 = new Circle("Zero Symphonic Battalion", 2010);
		session.save(c1);
		Circle c2 = new Circle("A-One", 2008);
		session.save(c2);
		Circle c3 = new Circle("TAMAONSEN", 2009);
		session.save(c3);
		Circle c4 = new Circle("TOKYO Active NEETs", 2008);
		session.save(c4);

		for(Artist ar : artist_list) {
			c1.addArtist(ar);
			session.save(ar);
		}
		session.save(c1);

		Event e1 = new Event("Comiket", 82);
		e1.setEvent_date(new GregorianCalendar(2012, 8-1, 11).getTime().getTime());
		e1.addAlias(new Alias("Comic Market"));
		e1.addAlias(new Alias("コミケット"));
		e1.addAlias(new Alias("コミックマーケット"));
		session.save(e1);

		Album al1 = new Album("Touhou Zerokyo Shinju ~ Sanctified Vengeance", "DCZ-002", "Doujin");
		session.save(al1);

		al1.addCircle(c1);
		al1.addCircle(c2);
		al1.addCircle(c3);
		al1.addCircle(c4);
		session.save(al1);

		e1.addAlbum(al1);
		session.save(e1);

		ArrayList<Music> music_list = new ArrayList<Music>();
		music_list.add(new Music("聖徳伝説 Rage end of mana", 1));
		music_list.add(new Music("詩仙打掛の四重奏", 2));
		music_list.add(new Music("De Sïdus", 3));
		music_list.add(new Music("猫街道から夜烏", 4));
		music_list.add(new Music("Little Bit Starmine", 5));
		music_list.add(new Music("Zonbie Apocalypse", 6));
		music_list.add(new Music("D3(D Cube)", 7));

		music_list.get(0).setFile(new File("file_path", 1024, 320));
		music_list.get(6).setFile(new File("file_path_2", 1024, 320));

		for(Music m : music_list) {
			al1.addMusic(m);
			Artist composer = (Artist) session.createCriteria(Artist.class).add(Restrictions.like("artist_name", "ZUN")).list().get(0);
			m.addComposer(composer);
			session.save(m);
		}
		session.save(al1);

		tx.commit();
		session.close();
	}

	public static void main(String[] args) {
		FirstConfiguration.clean();
		FirstConfiguration.build();
	}

}
